package org.amphiprion.diceroller.screens.animation;

import org.amphiprion.diceroller.TagObject;
import org.amphiprion.gameengine.Mesh;
import org.amphiprion.gameengine.animation.GameComponentAnimation;

import android.view.animation.Interpolator;

public class Rotate3DAnimation extends GameComponentAnimation {

	private float[] startAngle;
	private float[] endAngle;

	private Interpolator itp;
	private Mesh mesh;
	private TagObject tag;
	private boolean updateAlternate;

	public Rotate3DAnimation(Mesh mesh, boolean updateAlternate, long duration, long delay, float[] startAngle, float[] endAngle) {
		this.delay = delay;
		this.startAngle = startAngle;
		this.endAngle = endAngle;
		this.duration = duration;
		this.mesh = mesh;
		this.tag = (TagObject) mesh.getTagObject();
		this.updateAlternate = updateAlternate;
	}

	@Override
	protected float getInterpolatedProgress(float progress) {
		if (itp != null) {
			return itp.getInterpolation(progress);
		} else {
			return super.getInterpolatedProgress(progress);
		}
	}

	@Override
	protected void onUpdate(float progress) {
		if (updateAlternate) {
			tag.alternateRotation[0] = (endAngle[0] - startAngle[0]) * progress + startAngle[0];
			tag.alternateRotation[1] = (endAngle[1] - startAngle[1]) * progress + startAngle[1];
			tag.alternateRotation[2] = (endAngle[2] - startAngle[2]) * progress + startAngle[2];
		} else {
			mesh.rotXDegree = (endAngle[0] - startAngle[0]) * progress + startAngle[0];
			mesh.rotYDegree = (endAngle[1] - startAngle[1]) * progress + startAngle[1];
			mesh.rotZDegree = (endAngle[2] - startAngle[2]) * progress + startAngle[2];
		}
	}

	@Override
	public void start() {
	}

	public void setInterpolation(Interpolator itp) {
		this.itp = itp;
	}

	public void finish() {
		this.duration = 0;
		onUpdate(1.0f);
	}

	@Override
	public void removeComponent() {
		// TODO Auto-generated method stub

	}
}
